//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi;

import com.incors.plaf.alloy.AlloyLookAndFeel;
import com.incors.plaf.alloy.DefaultAlloyTheme;
import com.incors.plaf.alloy.themes.acid.AcidTheme;
import com.incors.plaf.alloy.themes.bedouin.BedouinTheme;
import com.incors.plaf.alloy.themes.glass.GlassTheme;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.gui.progressBar.ProgressBar;
import org.jpgolay.wi.agency.AgencyController;
import org.jpgolay.wi.customer.CustomerController;
import org.jpgolay.wi.diary.DiaryController;
import org.jpgolay.wi.group.GroupController;
import org.jpgolay.wi.persistence.ConnectDB;
import org.jpgolay.wi.persistence.UtilityDB;
import org.jpgolay.wi.preferences.PreferencesCRUD;
import org.jpgolay.wi.preferences.PreferencesController;
import org.jpgolay.wi.preferences.PreferencesGUI;
import org.jpgolay.wi.propertie.PropertyCRUD;
import org.jpgolay.wi.propertie.PropertyGUI;
import org.jpgolay.wi.realestate.RealEstateController;
import org.jpgolay.wi.realestate.RealEstateListController;
import org.jpgolay.wi.salesperson.SalesPersonController;
import org.jpgolay.wi.user.ConnectionGUI;
import org.jpgolay.wi.user.User;
import org.jpgolay.wi.user.UserCRUD;
import org.jpgolay.wi.user.UserController;
import org.jpgolay.wi.util.AboutGUI;
import org.jpgolay.wi.util.BrowserControl;
import org.jpgolay.wi.util.ChoiceFileListGUI;
import org.jpgolay.wi.util.Parameter;

// Referenced classes of package org.jpgolay.wi:
//            MainGUI
public class MainController {

    public MainController() {
        AlloyLookAndFeel.setProperty("alloy.licenseCode", "e#Technics_Services#dvjoxh#c54m1g");
        AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
        iconIF = new ImageIcon(getClass().getResource("images/logo2.gif"));
        init();
    }

    private void init() {
        con = new ConnectDB();
        if (con.isConnected()) {
            connectionGUI();
        } else {
            JOptionPane.showMessageDialog(null, "Impossible de d'obtenir une connexion sur la base de donn\351es !");
            int reponse = JOptionPane.showConfirmDialog(null, "Souhaitez vous configurer l'acc\350s \340 la base de donn\351e ?\n(Fonction r\351serv\351e \340 un Administrateur !)", "Confirmation", 1, 2, iconIF);
            switch (reponse) {
                case 0: // '\0'
                    dbAccess();
                    break;

                case 1: // '\001'
                    JOptionPane.showMessageDialog(null, "Si le probl\350me persist contactez votre administrateur r\351seau.");
                    close();
                    break;

                case 2: // '\002'
                    JOptionPane.showMessageDialog(null, "Si le probl\350me persist contactez votre administrateur r\351seau.");
                    close();
                    break;

                case -1:
                    JOptionPane.showMessageDialog(null, "Si le probl\350me persist contactez votre administrateur r\351seau.");
                    close();
                    break;
            }
        }
    }

    private void dbAccess() {
        prop = new PropertyCRUD();
        propGui = new PropertyGUI();
        PreferencesCRUD pref = new PreferencesCRUD();
        skinGUI(Integer.parseInt(pref.getLookAndFeel()), false);
        propGui.setDefaultCloseOperation(0);
        propGui.getCancelB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                close();
            }
        });
        propGui.getOkB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                propertyUpdate();
                init();
            }
        });
        propGui.setAdminPass(prop.getFirstPass());
        propGui.setAdminUrl(prop.getFirstUrl());
        propGui.setAdminUser(prop.getFirstUser());
        propGui.setDbBackup(prop.getBackupHome());
        propGui.setDbDriver(prop.getDriver());
        propGui.setDbHome(prop.getDataBaseHome());
        propGui.setIp(prop.getIP());
        propGui.setSerial(Integer.toString(prop.getSerial()));
        propGui.setWiPass(prop.getPass());
        propGui.setWiUrl(prop.getUrl());
        propGui.setWiUser(prop.getUser());
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        propGui.setBounds((screen.width - 500) / 2, (screen.height - 330) / 2, 500, 330);
        propGui.setVisible(true);
    }

    private void propertyUpdate() {
        UtilityDB uDB = null;
        prop.setFirstPass(propGui.getAdminPass());
        prop.setFirstUrl(propGui.getAdminUrl());
        prop.setFirstUser(propGui.getAdminUser());
        prop.setBackupHome(propGui.getDbBackup());
        prop.setDriver(propGui.getDbDriver());
        prop.setDataBaseHome(propGui.getDbHome());
        prop.setIp(propGui.getIp());
        try {
            prop.setSerial(Integer.parseInt(propGui.getSerial()));
        } catch (NumberFormatException e) {
            prop.setSerial(0);
        }
        prop.setPass(propGui.getWiPass());
        prop.setUrl(propGui.getWiUrl());
        prop.setUser(propGui.getWiUser());
        prop.update();
        if (propGui.isDbRemove()) {
            uDB = new UtilityDB();
            uDB.deleteMysqlDB();
        }
        if (propGui.isDbNew()) {
            if (uDB == null) {
                uDB = new UtilityDB();
            }
            uDB.createMysqlDB();
        }
        propGui.setVisible(false);
        propGui = null;
        prop = null;
    }

    private void connectionGUI() {
        PreferencesCRUD pref = new PreferencesCRUD();
        skinGUI(Integer.parseInt(pref.getLookAndFeel()), false);
        conGui = new ConnectionGUI();
        conGui.setDefaultCloseOperation(0);
        conGui.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                exit();
            }
        });
        conGui.getValiderB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                connection();
            }
        });
        conGui.getPasswordPF().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                connection();
            }
        });
        conGui.getAnnulerB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exit();
            }
        });
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        conGui.setBounds((screen.width - 350) / 2, (screen.height - 120) / 2, 350, 120);
        conGui.setVisible(true);
    }

    private void connection() {
        crud = new UserCRUD();
        String pass = "";
        char passtab[] = conGui.getPassword();
        for (int i = 0; i < passtab.length; i++) {
            pass = pass + passtab[i];
        }

        if (crud.findByUsernameAndPass(conGui.getUtilisateur(), pass)) {
            user = crud.getData();
            param = new Parameter();
            param.setAccessLaw(Long.toBinaryString(user.getAccess().intValue()));
            param.setUser(user);
            PreferencesCRUD pref = new PreferencesCRUD();
            param.setMinimumDecimalNumber(new Integer(pref.getNumberOfDecimal()));
            param.setRoundType(new Integer(pref.getRoundType()));
            param.setMinimumProfitMargin(pref.getMinimumProfitMargin());
            param.setPrintCustomerAssetLaw(pref.getPrintCustomerAssetLaw());
            param.setPrintCustomerBillLaw(pref.getPrintCustomerBillLaw());
            param.setPrintCustomerDeliveryLaw(pref.getPrintCustomerDeliveryLaw());
            param.setPrintCustomerOrderLaw(pref.getPrintCustomerOrderLaw());
            param.setPrintCustomerQuoteLaw(pref.getPrintCustomerQuoteLaw());
            param.setPrintSupplierAssetLaw(pref.getPrintSupplierAssetLaw());
            param.setPrintSupplierBillLaw(pref.getPrintSupplierBillLaw());
            param.setPrintSupplierOrderLaw(pref.getPrintSupplierOrderLaw());
            agencyId = crud.getAgencyId().toString();
            conGui.setVisible(false);
            conGui = null;
            initComponent();
            initListener();
            param.setDesktop(mGui.getMainDP());
        } else {
            conGui.initFields();
            JOptionPane.showMessageDialog(conGui, "Utilisateur inconnu !");
        }
    }

    private void initComponent() {
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        mGui = new MainGUI();
        mGui.setDefaultCloseOperation(0);
        PreferencesCRUD pref = new PreferencesCRUD();
        skinGUI(Integer.parseInt(pref.getLookAndFeel()), false);
        mGui.setLookAndFeel(Integer.parseInt(pref.getLookAndFeel()));
        mGui.setBounds(0, 0, screen.width, screen.height - 30);
        loadBackgroundImage();
        setLaw();
        mGui.setVisible(true);
    }

    private void loadBackgroundImage() {
        ImageIcon backgroudImage = new ImageIcon(getClass().getResource("images/splash.png"));
        JLabel label = new JLabel(backgroudImage);
        label.setBounds(screen.width / 2 - backgroudImage.getIconWidth() / 2, screen.height / 2 - (backgroudImage.getIconHeight() / 2 + 30), backgroudImage.getIconWidth(), backgroudImage.getIconHeight());
        mGui.getMainDP().add(label, new Integer(0x80000000));
    }

    private void initListener() {
        mGui.addWindowListener(new WindowAdapter() {
            public void windowIconified(WindowEvent evt) {
                iconifiedFramesChild();
            }

            public void windowDeiconified(WindowEvent evt) {
                deIconifiedFramesChild();
            }
        });
        mGui.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                exit();
            }
        });
        mGui.getRealEstateB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                realEstateGUI();
            }
        });
        mGui.getDiaryB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                diaryGUI();
            }
        });
        mGui.getRealEstate().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                realEstateGUI();
            }
        });
        mGui.getRealEstateMI().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                realEstateGUI();
            }
        });
        mGui.getRealEstateListB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                realEstateListGUI();
            }
        });
        mGui.getRealEstateList().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                realEstateListGUI();
            }
        });
        mGui.getHelpB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                helpGUI();
            }
        });
        mGui.getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exit();
            }
        });
        mGui.getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                helpGUI();
            }
        });
        mGui.getAbout().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                aboutGUI();
            }
        });
        mGui.getUser().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                userGUI();
            }
        });
        mGui.getAgency().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                agencyGUI();
            }
        });
        mGui.getGroup().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                groupGUI();
            }
        });
        mGui.getCustomer().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                customerGUI();
            }
        });
        mGui.getSalesPerson().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                salesPersonGUI();
            }
        });
        mGui.getGlassRBMI().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                skinGUI(0, true);
            }
        });
        mGui.getAcidRBMI().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                skinGUI(3, true);
            }
        });
        mGui.getMetalRBMI().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                skinGUI(4, true);
            }
        });
        mGui.getDefaultRBMI().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                skinGUI(1, true);
            }
        });
        mGui.getBedouinRBMI().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                skinGUI(2, true);
            }
        });
        mGui.getPreferences().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                preferencesGUI();
            }
        });
    }

    private void setLaw() {
        String b = param.getAccessLaw();
        int position = b.length();
        if (--position >= 0 && b.charAt(position) == '1') {
            mGui.realEstate.setEnabled(true);
            mGui.realEstateB.setEnabled(true);
            mGui.realEstateMI.setEnabled(true);
        }
        if ((position -= 2) >= 0 && b.charAt(position) == '1') {
            mGui.realEstateList.setEnabled(true);
            mGui.realEstateListB.setEnabled(true);
            mGui.diaryB.setEnabled(true);
        }
        if ((position -= 2) >= 0 && b.charAt(position) == '1') {
            mGui.customerMI.setEnabled(true);
        }
        if ((position -= 2) >= 0 && b.charAt(position) == '1') {
            mGui.salesPersonMI.setEnabled(true);
        }
        if ((position -= 2) >= 0 && b.charAt(position) == '1') {
            mGui.groupMI.setEnabled(true);
        }
        if ((position -= 2) >= 0 && b.charAt(position) == '1') {
            mGui.agencyMI.setEnabled(true);
        }
        if ((position -= 2) >= 0 && b.charAt(position) == '1') {
            mGui.userMI.setEnabled(true);
            mGui.getPreferences().setEnabled(true);
        }
        if ((position -= 2) >= 0) {
            b.charAt(position);
        }
        if ((position -= 2) >= 0) {
            b.charAt(position);
        }
        if ((position -= 2) >= 0) {
            b.charAt(position);
        }
        if ((position -= 2) >= 0) {
            b.charAt(position);
        }
        if (--position >= 0) {
            b.charAt(position);
        }
        if (--position >= 0) {
            b.charAt(position);
        }
        if (--position >= 0) {
            b.charAt(position);
        }
        if (--position >= 0) {
            b.charAt(position);
        }
        if (--position >= 0) {
            b.charAt(position);
        }
    }

    private void realEstateGUI() {
        if (reCtrl == null) {
            reCtrl = new RealEstateController(agencyId);
            reCtrl.setUser(user);
            reCtrl.setDefaultMainFrame(mGui);
            reCtrl.getOpen().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    choiceFileListGUI();
                }
            });
            ib = reCtrl.getIb();
            ib.addAnswerListener(new AnswerListener() {
                public void create(AnswerEvent answerevent) {
                }

                public void open(AnswerEvent e) {
                    choiceFileListGUI();
                }

                public void modify(AnswerEvent answerevent) {
                }

                public void save(AnswerEvent answerevent) {
                }

                public void remove(AnswerEvent answerevent) {
                }

                public void print(AnswerEvent answerevent) {
                }

                public void help(AnswerEvent answerevent) {
                }
            });
            mGui.getMainDP().add(reCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            reCtrl.maximizeFrame();
        } else {
            if (reCtrl.init()) {
                mGui.getMainDP().add(reCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
                reCtrl.maximizeFrame();
            }
            if (!reCtrl.isIb(ib)) {
                reCtrl.getOpen().addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        choiceFileListGUI();
                    }
                });
                ib = reCtrl.getIb();
                ib.addAnswerListener(new AnswerListener() {
                    public void create(AnswerEvent answerevent) {
                    }

                    public void open(AnswerEvent e) {
                        choiceFileListGUI();
                    }

                    public void modify(AnswerEvent answerevent) {
                    }

                    public void save(AnswerEvent answerevent) {
                    }

                    public void remove(AnswerEvent answerevent) {
                    }

                    public void print(AnswerEvent answerevent) {
                    }

                    public void help(AnswerEvent answerevent) {
                    }
                });
            }
        }
    }

    private void choiceFileListGUI() {
        if (cflGui == null) {
            cflGui = new ChoiceFileListGUI();
            cflGui.getListeB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    realEstateListGUI();
                }
            });
            cflGui.getNumFicheF().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    choiceFileList();
                }
            });
            cflGui.setBounds(screen.width / 2 - 110, screen.height / 2 - 100, 220, 140);
        }
        cflGui.setVisible(true);
    }

    private void realEstateListGUI() {
        if (cflGui != null) {
            cflGui.setVisible(false);
        }
        if (relCtrl == null) {
            relCtrl = new RealEstateListController(agencyId);
            relCtrl.setUser(user);
            relCtrl.setDefaultMainFrame(mGui);
            relCtrl.getTable().addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent evt) {
                    if (evt.getClickCount() == 2) {
                        resultatMouseClicked();
                    } else if (evt.getModifiers() == 16 && relCtrl.recherchePanel() == 3) {
                        resultatMouseClickedLeft();
                    }
                }
            });
            relCtrl.getTable().addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == 32) {
                        resultatMouseClicked();
                    }
                }
            });
            mGui.getMainDP().add(relCtrl.getVREL(), JLayeredPane.DEFAULT_LAYER);
            relCtrl.maximizeFrame();
        } else if (relCtrl.init()) {
            mGui.getMainDP().add(relCtrl.getVREL(), JLayeredPane.DEFAULT_LAYER);
            relCtrl.maximizeFrame();
            relCtrl.getTable().addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent evt) {
                    if (evt.getClickCount() == 2) {
                        resultatMouseClicked();
                    } else if (evt.getModifiers() == 16 && relCtrl.recherchePanel() == 3) {
                        resultatMouseClickedLeft();
                    }
                }
            });
            relCtrl.getTable().addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == 32) {
                        resultatMouseClicked();
                    }
                }
            });
        }
    }

    private void skinGUI(int type, boolean modify) {
        AlloyLookAndFeel lnf = new AlloyLookAndFeel();
        MetalLookAndFeel wlaf = null;
        switch (type) {
            case 0: // '\0'
                lnf.setTheme(new GlassTheme(), true);
                break;

            case 1: // '\001'
                lnf.setTheme(new DefaultAlloyTheme(), true);
                break;

            case 2: // '\002'
                lnf.setTheme(new BedouinTheme(), true);
                break;

            case 3: // '\003'
                lnf.setTheme(new AcidTheme(), true);
                break;

            case 4: // '\004'
                wlaf = new MetalLookAndFeel();
                break;
        }
        try {
            if (type == 4) {
                UIManager.setLookAndFeel(wlaf);
                if (mGui != null) {
                    SwingUtilities.updateComponentTreeUI(mGui);
                }
            } else {
                UIManager.setLookAndFeel(lnf);
                AlloyLookAndFeel.updateAllUIs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (modify) {
            PreferencesCRUD pref = new PreferencesCRUD();
            pref.setLookAndFeel(Integer.toString(type));
            pref.update();
        }
    }

    private void aboutGUI() {
        if (abGui == null) {
            abGui = new AboutGUI();
            if (user.getGroupId() == null) {
                abGui.setLiscenceNumberL("Cette version de WEBIMMO n'est pas configurer correctement");
            } else {
                abGui.setLiscenceNumberL("Cette version de WEBIMMO est configurer pour le groupe  " + user.getGroupId());
            }
            abGui.getOkB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    abGui.setVisible(false);
                    abGui = null;
                }
            });
            abGui.setBounds(screen.width / 2 - 302, screen.height / 2 - 280, 604, 500);
        }
        abGui.setVisible(true);
    }

    private void helpGUI() {
        BrowserControl.displayURL("www.ifacile.com");
    }

    private void userGUI() {
        if (uCtrl == null) {
            uCtrl = new UserController(agencyId);
            uCtrl.setUser(user);
            uCtrl.setJDesktopPane(mGui.getMainDP());
            mGui.getMainDP().add(uCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            uCtrl.maximizeFrame();
        } else if (uCtrl.init()) {
            mGui.getMainDP().add(uCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            uCtrl.maximizeFrame();
        }
    }

    private void agencyGUI() {
        if (aCtrl == null) {
            aCtrl = new AgencyController();
            aCtrl.setUser(user);
            aCtrl.setJDesktopPane(mGui.getMainDP());
            mGui.getMainDP().add(aCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            aCtrl.maximizeFrame();
        } else if (aCtrl.init()) {
            mGui.getMainDP().add(aCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            aCtrl.maximizeFrame();
        }
    }

    private void groupGUI() {
        if (gCtrl == null) {
            gCtrl = new GroupController();
            gCtrl.setUser(user);
            gCtrl.setDefaultMainFrame(mGui);
            mGui.getMainDP().add(gCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            gCtrl.maximizeFrame();
        } else if (gCtrl.init()) {
            mGui.getMainDP().add(gCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            gCtrl.maximizeFrame();
        }
    }

    private void customerGUI() {
        mGui.setCursor(new Cursor(3));
        if (customerCtrl == null || customerCtrl.isClosed()) {
            customerCtrl = null;
            customerCtrl = new CustomerController(param);
            customerCtrl.setIconifiable(true);
            customerCtrl.setResizable(false);
            customerCtrl.setMaximizable(false);
            customerCtrl.setClosable(true);
            screen = mGui.getMainDP().getSize();
            customerCtrl.setBounds(screen.width / 2 - 170, screen.height / 2 - 241, 340, 482);
            mGui.getMainDP().add(customerCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (customerCtrl.isIcon()) {
            try {
                customerCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!customerCtrl.isVisible()) {
            customerCtrl.setVisible(true);
        }
        customerCtrl.toFront();
        mGui.setCursor(new Cursor(0));
    }

    private void salesPersonGUI() {
        mGui.setCursor(new Cursor(3));
        if (salesPersonCtrl == null || salesPersonCtrl.isClosed()) {
            salesPersonCtrl = null;
            salesPersonCtrl = new SalesPersonController(param);
            salesPersonCtrl.setIconifiable(true);
            salesPersonCtrl.setResizable(false);
            salesPersonCtrl.setMaximizable(false);
            salesPersonCtrl.setClosable(true);
            screen = mGui.getMainDP().getSize();
            salesPersonCtrl.setBounds(screen.width / 2 - 165, screen.height / 2 - 255, 330, 510);
            mGui.getMainDP().add(salesPersonCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (salesPersonCtrl.isIcon()) {
            try {
                salesPersonCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!salesPersonCtrl.isVisible()) {
            salesPersonCtrl.setVisible(true);
        }
        salesPersonCtrl.toFront();
        mGui.setCursor(new Cursor(0));
    }

    private void diaryGUI() {
        mGui.setCursor(new Cursor(3));
        if (diaryCtrl == null || diaryCtrl.isClosed()) {
            diaryCtrl = null;
            diaryCtrl = new DiaryController(param);
            diaryCtrl.setIconifiable(true);
            diaryCtrl.setResizable(false);
            diaryCtrl.setMaximizable(false);
            diaryCtrl.setClosable(true);
            screen = mGui.getMainDP().getSize();
            diaryCtrl.setBounds(0, 0, screen.width, screen.height);
            mGui.getMainDP().add(diaryCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (diaryCtrl.isIcon()) {
            try {
                diaryCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!diaryCtrl.isVisible()) {
            diaryCtrl.setVisible(true);
        }
        diaryCtrl.toFront();
        mGui.setCursor(new Cursor(0));
    }

    private void preferencesGUI() {
        mGui.setCursor(new Cursor(3));
        if (preferencesCtrl == null) {
            preferencesCtrl = new PreferencesController(param);
            mGui.getMainDP().add(preferencesCtrl.getPreferencesGUI(), JLayeredPane.DEFAULT_LAYER);
        } else {
            mGui.getMainDP().add(preferencesCtrl.getPreferencesGUI(), JLayeredPane.DEFAULT_LAYER);
        }
        try {
            preferencesCtrl.getPreferencesGUI().setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        mGui.setCursor(new Cursor(0));
    }

    private void choiceFileList() {
        cflGui.setVisible(false);
        switch (cflGui.getChoice()) {
            case 0: // '\0'
                reCtrl.setId(cflGui.getNumFiche());
                break;

            case 1: // '\001'
                reCtrl.setInternalReference(cflGui.getNumFiche());
                break;
        }
    }

    private void printingModel() {
    }

    private void resultatMouseClicked() {
        realEstateGUI();
        reCtrl.setId((String) relCtrl.getTable().getValueAt(relCtrl.getTable().getSelectedRow(), 0));
    }

    private void resultatMouseClickedLeft() {
        relCtrl.setActionPanel((String) relCtrl.getTable().getValueAt(relCtrl.getTable().getSelectedRow(), 0));
    }

    private void startProgressBar() {
        if (pb == null) {
            pb = new ProgressBar(mGui, false);
            pb.setLocationRelativeTo(mGui);
            pb.setVisible(true);
            pb.start();
        } else {
            pb.setVisible(true);
            pb.start();
        }
    }

    private void stopProgressBar() {
        if (!pb.isStop()) {
            pb.setVisible(false);
            pb.stop();
        }
    }

    private void iconifiedFramesChild() {
        if (relCtrl != null) {
            relCtrl.iconified();
        }
        if (reCtrl != null) {
            reCtrl.iconified();
        }
    }

    private void deIconifiedFramesChild() {
        if (relCtrl != null) {
            relCtrl.unIconified();
        }
        if (reCtrl != null) {
            reCtrl.unIconified();
        }
    }

    private void exit() {
        int reponse = JOptionPane.showConfirmDialog(null, "Souhaitez-vous vraiment quitter WebImmo ?", "Confirmation", 1, 2, iconIF);
        switch (reponse) {
            case 0: // '\0'
                close();
            // fall through

            case -1:
            case 1: // '\001'
            case 2: // '\002'
            default:
                return;
        }
    }

    private void close() {
        System.exit(0);
    }

    public static void main(String args[]) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        MainController mainController = new MainController();
    }
    private MainGUI mGui;
    private UserCRUD crud;
    private User user;
    private IconBar ib;
    private ConnectionGUI conGui;
    private AboutGUI abGui;
    private ChoiceFileListGUI cflGui;
    private ProgressBar pb;
    private ConnectDB con;
    private PropertyCRUD prop;
    private PropertyGUI propGui;
    private RealEstateController reCtrl;
    private RealEstateListController relCtrl;
    private UserController uCtrl;
    private AgencyController aCtrl;
    private GroupController gCtrl;
    private CustomerController customerCtrl;
    private SalesPersonController salesPersonCtrl;
    private DiaryController diaryCtrl;
    private PreferencesController preferencesCtrl;
    private Dimension screen;
    private Dimension deskSize;
    private ImageIcon iconIF;
    private String agencyId;
    private Parameter param;
}
